(function ($) {
    /*
     * Plugin zRefresh
     * Lam moi noi dung voi ajax
     * @author Chuột
     * Version 1.0
     */
    $.fn.zRefresh = function (options) {
        //=============================================
        //Cac gia mac dinh cua doi tuong options
        //=============================================
        var defaults = {
            "paging": "#paging", //id tag a paging
            "content": "#content", //id content table
            "reset": "#reset", //id button reset
            "main": "#main", //id main of page
            "text": "#text", //id text search
            "btnsearch": "#btnsearch", //id button search
            "urlsearch": "#urlsearch", //id tag a url search
            "btnadd": "#btnadd", //id button add new
            "details": ".details", //class tag a details
            "back": "#back", //id tag a back to index
            "year": ".year", //class tag select options
            "doantt": "#doantt" //id content doan tham gia
        };
        //Ap dung defaults vao options
        options = $.extend(defaults, options);
        //=============================================
        //Cac bien se su dung trong plugin
        //=============================================
        var paging = $(options.paging);
        var content = $(options.content);
        var reset = $(options.reset);
        var main = $(options.main);
        var text = $(options.text);
        var btnsearch = $(options.btnsearch);
        var urlsearch = $(options.urlsearch);
        var btnadd = $(options.btnadd);
        var details = $(options.details);
        var back = $(options.back);
        var year = $(options.year);
        var doantt = $(options.doantt);
        //=============================================
        //Khoi tao cac ham can thi khi Plugin duoc su dung
        //=============================================
        init();
        //=============================================
        //Ham khoi dong
        //=============================================
        function init() {
            //Clicking in tag a paging
            $(paging).on("click", function (e) {
                var url = $(this).attr('href');
                refreshGet(url, content);
                e.stopImmediatePropagation();
                return false;
            });
            //Clicking button or text search
            $(text).keyup(function (e) {
                if (e.which === 13) {
                    search(urlsearch, text, reset);
                    e.stopImmediatePropagation();
                    return false;
                }
            });
            $(btnsearch).click(function (e) {
                search(urlsearch, text, reset);
                e.stopImmediatePropagation();
                return false;
            });
            //Clicking button reset
            $(reset).on("click", function (e) {
                var url = $(this).attr('href');
                refreshGet(url, content);
                e.stopImmediatePropagation();
                text.val("");
                reset.hide().fadeOut("slow");
                return false;
            });
            //Clicking button add new
            $(btnadd).on("click", function (e) {
                var url = $(this).attr('href');
                refreshGet(url, main);
                e.stopImmediatePropagation();
                return false;
            });
            //Clicking tag a back to index
            $(back).on("click", function (e) {
                var url = $(this).attr('href');
                refreshGet(url, main);
                e.stopImmediatePropagation();
                return false;
            });
            //Clicking tag view details
            $(details).on("click", function (e) {
                var id = $(this).attr("id");
                var url = $(this).attr("href") + "/" + id;
                refreshGet(url, main);
                e.stopImmediatePropagation();
                return false;
            });
            //Clicking tag select option
            $(year).on("change", function (e) {
                var idcb = $(this).attr("id");
                var idnam = $(this).children(":selected").attr("id");
                var url = $(this).children(":selected").attr("value");
                var data = "idcb=" + idcb + "&idnam=" + idnam;
                refreshPost(url, data, doantt);
                e.stopImmediatePropagation();
                return false;
            });

        }
        //=============================================
        //Ham xu ly khi co su kien voi phuong thuc get
        //=============================================
        function refreshGet(url, content)
        {
            $.ajax(
                    {
                        url: url,
                        type: "get",
                        datatype: "html"
                    }).done(function (data) {
                $(content).hide().fadeIn("slow").html(data.html);
            }).fail(function (jqXHR, ajaxOptions, thrownError)
            {
                toastr.error('No response from server!');
            });
        }
        //=============================================
        //Ham xu ly khi co su kien voi phuong thuc post
        //=============================================
        function refreshPost(url, data, content)
        {
            $.ajax(
                    {
                        url: url,
                        type: "post",
                        data: data,
                        beforeSend: function (request) {
                            return request.setRequestHeader('X-CSRF-Token', $("meta[name='token']").attr('content'));
                        },
                        datatype: "html"
                    }).done(function (data) {
                $(content).hide().fadeIn("slow").html(data.html);
            }).fail(function (jqXHR, ajaxOptions, thrownError)
            {
                toastr.error('No response from server!');
            });
        }
        //=============================================
        //Ham xu ly khi nhan nut tim kiem
        //=============================================
        function search(urlsearch, text, reset)
        {
            var searchtxt = text.val();
            var url = urlsearch.val();
            searchtxt = fulltrim(searchtxt);
            if (searchtxt === null || searchtxt === "")
            {
                toastr.error("Vui lòng nhập chuỗi cần tìm kiếm!");
            }
            else
            {
                url = url + "/" + searchtxt;
                refreshGet(url, content);
                $(reset).show().fadeIn("slow");
                return false;
            }
        }
        //=============================================
        //Ham cat khoang trang toan chuoi
        //=============================================
        function fulltrim(str)
        {
            return  str.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g, '').replace(/\s+/g, ' ');
        }
    }
})(jQuery);